
#include "simi2c.h"

#include "delay.h"
#include "stdio.h"
#include "bsp_exti.h"
#include "sys.h"
#include "stm32f4xx.h"

//1.定时器-pwm配置函数
//beep警报音 - 设备链接到了哪个引脚？
//moto模块 - 设备链接到了哪个引脚？
//定时器2的通道3驱动蜂鸣器


//beep pwm驱动函数
//void BEEP_PWM_DRIVER()
//{
//	unsigned int value = 0 ,flg = 1 ;
//	//value 0 - 60 - 0 
//	int num = 100;
//	while(num--)
//	{
//		if(flg == 1)
//			value++;
//		else
//			value--;
//		
//		//判断范围
//		if(value > 600)
//			flg = 0 ; 
//		if(value == 0 )
//			flg = 1;
//		//修改捕获比较寄存器的数值
//		TIM_SetCompare3(TIM2,value);
//		delay_ms(10);
//	}
//}

//2.舵机模块
//定时周期在20ms>>> 总线时钟/预分频  + 壮哉数值---准备一个合适数值
//高电平时间     0.5ms- 1ms - 1.5m - 2ms - 2.5ms之间
//舵机转动角度是 0度 - 45---90---135-- 180度；
void TIM4_PWM_CH1_MOTOR_CONFIG()
{
	//1.配置时钟 - timer + GPIO 
	//84MHZ  GPIOD 12 ; 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
	
	//2.GPIOD - 12 ; 
	GPIO_InitTypeDef motostruct;
	//引脚
	motostruct.GPIO_Pin = GPIO_Pin_12;
	//复用模式
	motostruct.GPIO_Mode = GPIO_Mode_AF;
	//上拉电阻
	motostruct.GPIO_PuPd = GPIO_PuPd_UP;
	//低速
	motostruct.GPIO_Speed = GPIO_Low_Speed;
	GPIO_Init(GPIOD,&motostruct);
	
	//3.引脚复用映射  PD12 - TIM2
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);
	
	//4.配置定时器
	TIM_TimeBaseInitTypeDef timstruct;
	//自动装载数值 - 定时周期确定
	timstruct.TIM_Period = 20000 - 1; //定时周期20ms
	//分频系数
	//84M / 84 = 1 M
	//1000000hz  周期/秒
	//一个周期: 1us
	//定时时间 20ms = 20000us / 1 = 20000 个数值。
	timstruct.TIM_Prescaler = 84 -1 ; 
	timstruct.TIM_CounterMode = TIM_CounterMode_Up;
	timstruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM4,&timstruct);
	
	//5 配置通道
	TIM_OCInitTypeDef oc1struct;
	//cnt小于 ccr3 - 是有效电平
	oc1struct.TIM_OCMode = TIM_OCMode_PWM1;
	//高电平是有效电平
	oc1struct.TIM_OCPolarity = TIM_OCPolarity_High;
	//使能
	oc1struct.TIM_OutputState = TIM_OutputState_Enable;
	//初始捕获比较寄存器的数值
	oc1struct.TIM_Pulse = 0 ; 
	TIM_OC1Init(TIM4,&oc1struct);

	//6 比较捕获寄存器使能
	TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
	
	//7 自动装载数值寄存器的使能
	TIM_ARRPreloadConfig(TIM4,ENABLE);
	
	//8 定时器本身的使能
	TIM_Cmd(TIM4,ENABLE);
}

//
void MOTO_LEFT()
{
	//500 * 1us = 0.5ms
	TIM_SetCompare1(TIM4,500);
	delay_ms(1000);
}

void MOTO_RIGHT()
{
	TIM_SetCompare1(TIM4,5500);
	delay_ms(1000);
}
	
//3.直流电机模块
void TIM12_PWM_CH2_FAN_CONFIG()
{
    GPIO_InitTypeDef GPIOB151struct;  // 将变量声明移到函数开头
    TIM_TimeBaseInitTypeDef timstruct;
    TIM_OCInitTypeDef oc2struct;

    // 启用时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    // 配置GPIOB Pin 15
    GPIOB151struct.GPIO_Pin = GPIO_Pin_15;
    GPIOB151struct.GPIO_Mode = GPIO_Mode_AF;
    GPIOB151struct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIOB151struct.GPIO_Speed = GPIO_Low_Speed;
    GPIO_Init(GPIOB, &GPIOB151struct);

    // 配置引脚复用
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM12);

    // 配置定时器基本参数
    timstruct.TIM_Prescaler = 84 - 1;
    timstruct.TIM_Period = 20000 - 1;
    timstruct.TIM_CounterMode = TIM_CounterMode_Up;
    timstruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM12, &timstruct);

    // 配置PWM通道
    oc2struct.TIM_OCMode = TIM_OCMode_PWM1;
    oc2struct.TIM_OCPolarity = TIM_OCPolarity_High;
    oc2struct.TIM_OutputState = TIM_OutputState_Enable;
    oc2struct.TIM_Pulse = 0;
    TIM_OC2Init(TIM12, &oc2struct);

    // 使能预装载和定时器
    TIM_OC2PreloadConfig(TIM12, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM12, ENABLE);
    TIM_Cmd(TIM12, ENABLE);
}
//风扇控制函数-打开风扇
void Fan_Control(unsigned int cvalue)
{    
    TIM_SetCompare2(TIM12,cvalue);
}
// 关闭风扇
void Fan_Stop()
{
    // 将比较捕获寄存器的值设置为 0，使风扇停止
    TIM_SetCompare2(TIM12, 0);
}
